package com.itzjc.service.impl;

import com.itzjc.dao.UserMapper;
import com.itzjc.domain.entity.RequestUser;
import com.itzjc.domain.pojo.User;
import com.itzjc.service.UserService;
import com.itzjc.utils.CommonUtils;
import com.itzjc.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * TODO
 *
 * @className: UserServiceImpl
 * @date: 2021-06-10 09:25
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public User findById(int id) {
        return userMapper.findById(id);
    }

    @Override
    public User login(String phone, String password) {
        User user = userMapper.login(phone, CommonUtils.MD5(password));
        if (null == user) {
            User user1=new User();
            user1.setToken("0");
            return user1;
        } else {
            String token = JWTUtils.geneJsonWebToken(user);
            user.setToken(token);
            user.setPassword("");
            return user;
        }


    }

    @Override
    @Transactional(transactionManager = "test1TransactionManager")
    public String register(RequestUser requestUser) {
        User user = new User();
        user.setUserName(requestUser.getUserName());
        user.setPhone(requestUser.getPhone());
        user.setPassword(CommonUtils.MD5(requestUser.getPassword()));
        user.setCreateTime(new Date());
        user.setHeadImg(getRandomImg());
        //默认是4没有权限状态，3才是授权的状态
        user.setStatus(4);
        user.setEmail(requestUser.getEmail());
        //查询TeacherBelong，这是修改密码必备的，这里的teacherBelong字段改成了验证码
        String teacherBelong=userMapper.findTeacherBelong();
        user.setTeacherBelong(teacherBelong);
        if (null!=requestUser.getPlaceBelong()) {
            user.setPlaceBelong(requestUser.getPlaceBelong());
        }
        //判断有没有注册手机号
        User user1= userMapper.findPhone(user.getPhone());
        if(null!=user1){
            return null;
        }

        //如果用户正常就写到数据库中
        if (null != user) {
            //这一步如果完成，就直接把名称返回就行了，不用再查了，用事物控制起来就行了
            userMapper.register(user);
            String username = requestUser.getUserName();
            return username;
        } else {
            return null;
        }
    }

    @Override
    public String forget(RequestUser requestUser) {
        User user = new User();
        user.setPhone(requestUser.getPhone());
        User user1= userMapper.findPhone(user.getPhone());
        if (null != user1) {
            //如果有用户就开始校验验证码是否正确,这里的teacherBelong字段改成了验证码
            String code=userMapper.findTeacherBelong();
            if(code.equals(requestUser.getTeacherBelong())){
                user1.setPassword(CommonUtils.MD5(requestUser.getPassword()));
                userMapper.forget(user1);
                String phone = user1.getUserName();
                return phone;
            }else{
                return null;
            }

        } else {
            return null;
        }


    }

    @Override
    public User userInfoByToken(Integer userId) {
        return userMapper.findById(userId);
    }

    @Override
    public String phoneNumberIsOk(RequestUser requestUser) {
        User user = new User();
        user.setPhone(requestUser.getPhone());
        User user1= userMapper.findPhone(user.getPhone());
        if(null!=user1){
            return null;
        }
        return "没有重复手机号";

    }


    /**
     * 定义头像地址的静态变量
     * 资源来自网络共享的阿里云文件服务器
     *
     * @return:
     **/
    private static final String[] headImg = {
            "http://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/17.jpeg",
            "http://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/12.jpeg",
            "http://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/13.jpeg",
            "http://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/14.jpeg",
            "http://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/default/head_img/15.jpeg"
    };

    private String getRandomImg() {
        int size = headImg.length;
        Random random = new Random();
        int index = random.nextInt(size);
        return headImg[index];
    }
}
